/**
 * 用来判断是否需要后续的项
 * @param array
 * @param options
 * @param judgeConditionFn
 * @param childrenKey
 * @returns
 */
export function getNewArrByCondition<Value, Options>(array: Value[], options: Options[],
  judgeConditionFn: (value: Value, options: Options) => boolean,
  childrenKey: string = 'children',): [Value[], Options[], number] {
  const newArr: Value[] = [];
  const newOptionsArr: Options[] = [];
  let curIndex: number = 0;
  const judgeArr = (optionsArr: Options[]) => {
    optionsArr.forEach((item) => {
      // 判断成功
      if (judgeConditionFn(array[curIndex], item) && curIndex < array.length) {
        newArr.push(array[curIndex])
        newOptionsArr.push(item)
        if (curIndex < array.length - 1) {
          curIndex++;
        }
        if (Array.isArray(item?.[childrenKey])) {
          judgeArr(item[childrenKey])
        }
      }
    })
  }
  judgeArr(options);
  return [newArr, newOptionsArr, curIndex];
}

export function transObjToNewKey(arr, keyObj, childrenKey = 'children') {
  return arr.map((obj) => {
    const newObj = {}
    for (const key in obj) {
      const newKey = keyObj?.[key] ?? key;
      if (key === childrenKey && Array.isArray(obj[key]) && obj[key].length) {
        newObj[newKey] = transObjToNewKey(obj[key], keyObj, childrenKey)
      } else {
        newObj[newKey] = obj[key];
      }
    }
    return newObj
  });
}
